package com.agent.crm.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.agent.crm.base.BaseActivity;

public class SettingActivity extends BaseActivity {

	private TextView start_on_boot;
	private TextView auto_login;
	private TextView edit_password;
	private TextView auto_clean_schedule;
	private TextView logout;
	private SharedPreferences preferences ;
	private Button back;
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.start_on_boot:
			final CheckBox checkBoxBoot =new CheckBox(this);
			boolean startAppWhenBoot= preferences.getBoolean("startAppWhenBoot", true);
			checkBoxBoot.setChecked(startAppWhenBoot);
			checkBoxBoot.setText("开机启动");
            new AlertDialog.Builder(this)
            .setTitle("是否开机启动")
            .setView(checkBoxBoot)
            .setPositiveButton("确定", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Editor editor =preferences.edit();
					if(checkBoxBoot.isChecked()){
						editor.putBoolean("startAppWhenBoot",true);
					}else{
						editor.putBoolean("startAppWhenBoot",false);
					}
					editor.commit();
				}
			}).show();
			break;
		case R.id.auto_login:
			final CheckBox checkBoxLogin =new CheckBox(this);
			boolean isAutoLogin= preferences.getBoolean("isAutoLogin", false);
			checkBoxLogin.setChecked(isAutoLogin);
			checkBoxLogin.setText("自动登录");
            new AlertDialog.Builder(this)
            .setTitle("是否自动登录")
            .setView(checkBoxLogin)
            .setPositiveButton("确定", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Editor editor =preferences.edit();
					if(checkBoxLogin.isChecked()){
						editor.putBoolean("isAutoLogin",true);
					}else{
						editor.putBoolean("isAutoLogin",false);
					}
					editor.commit();
				}
			}).show();

			break;
		case R.id.edit_password:
			final LinearLayout layout =new LinearLayout(this);
			LayoutInflater inflater = LayoutInflater.from(this);
			inflater.inflate(R.layout.edit_password, layout);
			final EditText old_password =(EditText)layout.findViewById(R.id.old_password);
			final EditText new_password =(EditText)layout.findViewById(R.id.new_password);
			final EditText confirm_password =(EditText)layout.findViewById(R.id.confirm_password);
			new AlertDialog.Builder(this)
            .setTitle("是否自动登录")
            .setView(layout)
            .setPositiveButton("确定", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Editor editor =preferences.edit();
					if(old_password.getText().toString().equals(preferences.getString("password", "123456"))){
						if(new_password.getText().toString().equals(confirm_password.getText().toString())){
							editor.putString("password",new_password.getText().toString());
							showSimpleAlertDialog("提示","密码修改成功！！");
						}else{
							showSimpleAlertDialog("提示","新密码和确认密码不匹配！！");
						}
					}else{
						showSimpleAlertDialog("提示","旧密码输入错误请检查！！");
					}
					editor.commit();
				}
			}).setNegativeButton("取消", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			}).show();
			break;
		case R.id.auto_clean_schedule:
			final CheckBox checkBoxClean =new CheckBox(this);
			boolean isAutoClean= preferences.getBoolean("isAutoClean", false);
			checkBoxClean.setChecked(isAutoClean);
			checkBoxClean.setText("自动清理");
            new AlertDialog.Builder(this)
            .setTitle("是否自动清理已完成任务和过期任务")
            .setView(checkBoxClean)
            .setPositiveButton("确定", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Editor editor =preferences.edit();
					if(checkBoxClean.isChecked()){
						editor.putBoolean("isAutoClean",true);
					}else{
						editor.putBoolean("isAutoClean",false);
					}
					editor.commit();
				}
			}).show();
			break;
		case R.id.logout:
            new AlertDialog.Builder(this)
            .setTitle("提示")
            .setMessage("你确定要退出登陆吗？")
            .setPositiveButton("确定", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Editor editor =preferences.edit();
					editor.putString("isLogin", "false");
					editor.commit();
					Intent intent = new Intent();
	    			intent.setClass(SettingActivity.this, LoginActivity.class);
	    			startActivity(intent);
					finish();
				}
			}).setNegativeButton("取消", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			}).show();
			break;
		case R.id.back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected int initLayout() {
		// TODO Auto-generated method stub
		return R.layout.setting;
	}

	@Override
	protected View initCustomLayout() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void initViewCmp() {
		// TODO Auto-generated method stub
		start_on_boot=(TextView)findViewById(R.id.start_on_boot);
		auto_login=(TextView)findViewById(R.id.auto_login);
		edit_password=(TextView)findViewById(R.id.edit_password);
		auto_clean_schedule=(TextView)findViewById(R.id.auto_clean_schedule);
		logout=(TextView)findViewById(R.id.logout);
		back=(Button)findViewById(R.id.back);
		((TextView)findViewById(R.id.title_txt)).setText("设置");
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		start_on_boot.setOnClickListener(this);
		auto_login.setOnClickListener(this);
		edit_password.setOnClickListener(this);
		auto_clean_schedule.setOnClickListener(this);
		logout.setOnClickListener(this);
		back.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		preferences=getSharedPreferences("login_data", Context.MODE_PRIVATE);
	}

	@Override
	protected void initAsycnData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void changeDefaultSetting() {
		// TODO Auto-generated method stub

	}

}
